package api_.collectionTest;


import java.util.Arrays;

public class SimpleArrayList
{
    private Object[] data;
    private int size;

    public SimpleArrayList()
    {
//        data = new Object[2];
        this(16);
    }
    public SimpleArrayList(int cap)
    {
        data = new Object[cap];
    }

    @Override
    public String toString()
    {
        StringBuilder temp = new StringBuilder("[");
        for (int i = 0 ; i < size ; i++)
        {
            temp.append(data[i]).append(",");
        }
        temp.deleteCharAt(temp.lastIndexOf(","));
        temp.append("]");
        return temp.toString();
    }

    public void add(Object o)
    {
        if(data.length == size)
        {
            // 扩容，扩两倍
            System.out.println("扩容");
            data =  Arrays.copyOf(data,data.length*2);
        }
        else
        {
            data[size++] = o;
        }

    }

    public int size()
    {
        return size;
    }
/*
    public boolean remove(Object o)
    {
        int index = 0;
        if(size == 0)
            return false;
        else
        {
            // 找到要删除的元素
            for (int i = 0 ; i < size ; i++)
            {
                if(data[i].equals(o))
                {
                    index = i;
                    break;
                }
            }
        }

        if(index == size-1 && !data[index].equals(o))
        {
            return false;
        }


        for (int i = index ; i < size ; i ++)
        {
            data[i] = data[i+1];
        }
        size--;
        return true;
    }
*/
    public boolean remove(Object object)
    {
        boolean flag = false;
        if(size == 0)
            return false;

        for (int i = 0 ; i < size ; i++)
        {
            if(data[i].equals(object))
            {
                flag = true;
                System.arraycopy(data,i+1,data,i,size-i-1);
                break;
            }
        }
        data[--size] = null;

        return flag;
    }


    public void add(int index,Object element)
    {
        if (index <0 || index > size)
            return;

        System.arraycopy(data,index,  data,index+1,size-index);
        data[index] = element;
        size++;
    }

    public boolean contains(Object obj)
    {
        // 用于判断集合中是否存在obj

        if(size == 0 || obj == null)
            return false;

        boolean flag = false;
        for (int i = 0 ; i < size ; i++)
        {
            if(data[i].equals(obj))
                flag = true;
        }
        return flag;
    }


    public Object remove(int index)
    {
        if(size == 0 || (index < 0 || index > size-1))
            return null;
        // 用于删除指定位置的数据
        Object result = null;
            result = data[index];
            System.arraycopy(data,index+1,data,index,size-1-index);
        size--;
        return result;
    }

    void clear()
    {
        for (int i = 0 ; i < size ; i++)
        {
            data[i] = null;
        }
//        size = 0;
    }
}
